package com.hup.utils.swing.customView;

import com.hup.utils.swing.ComponentUtil;
import com.hup.utils.swing.GraphicsUtil;

import javax.swing.JComponent;
import javax.swing.JLabel;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * bug:半透明背景时,实际背景色会变黑
 * 修正JLabel半透明时,有绘制残留问题;
 * .使用这个方法更新背景色:{@link ComponentUtil#setBackground(JComponent, Color)}
 * .本类可作 AlphaComposite.Src 的使用例子
 *
 * @author hugan
 * @date 2020/11/23
 * @deprecated 改用 {@link ColorBackgroundLabel}
 */
@Deprecated
public class OpaqueLabel extends JLabel {

    @Override
    public void paint(Graphics g0) {
        if (!isOpaque()) {
            //透明时,按默认方法
            super.paint(g0);
            return;
        }

        //不透明,解决JLabel的绘制残留:先用全透明像素覆盖整个背景,再super.paint
        Graphics2D g = GraphicsUtil.antiAlias((Graphics2D) g0);
        Composite defComposite = g.getComposite(); //默认是SRC_OVER:绘制时将当前颜色,叠加在原有颜色上
        g.setComposite(AlphaComposite.Src);//直接将像素点,设为当前颜色
        g.setColor(new Color(0, 0, 0, 0));
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setComposite(defComposite);

        super.paint(g0);
    }
}
